package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

/**
 * 我的问答适配器
 * 
 * @author lkx Created at 2015-11-20 下午3:42:03
 * @version 1.0
 */
public class MyAskAndAnswerAdapter extends BaseAdapter {
	JSONArray array;
	Activity activity;
	LayoutInflater inflater = null;
	// protected ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;

	public MyAskAndAnswerAdapter(Activity activity, JSONArray array) {
		this.activity = activity;
		this.array = array;
		inflater = LayoutInflater.from(activity);

	}

	public MyAskAndAnswerAdapter(Activity activity) {
		this.activity = activity;
		inflater = LayoutInflater.from(activity);

	}

	@Override
	public int getCount() {

		// TODO Auto-generated method stub
		return array == null ? 0 : array.length();
		// return 5;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.infox_activity_myanswer_messagelist_item, null);
			holder.itemimage = (ImageView) convertView.findViewById(R.id.id_image);
			holder.itemMassagetime = (TextView) convertView.findViewById(R.id.massagetime);
			holder.itemMassageData = (TextView) convertView.findViewById(R.id.massageData);
			holder.itemMassageFrom = (TextView) convertView.findViewById(R.id.massageFrom);
			holder.linlayout = (RelativeLayout) convertView.findViewById(R.id.meaasgeItem);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		try {
			
			
			holder.itemMassagetime.setText(AppKit.timeToBeforeStr(array.getJSONObject(position).getLong("time")));
			// AppKit.timeToBeforeStr(array.getJSONObject(position).getLong("time"))
			// string 类型时间
			//holder.itemMassagetime.setText(array.getJSONObject(position).getString("time"));
			holder.itemMassageFrom.setText(array.getJSONObject(position).getJSONObject("answer").getString("mname"));
			holder.itemMassageData.setText(array.getJSONObject(position).getJSONObject("issue").getString("content"));

			String imageurl = array.getJSONObject(position).getJSONObject("answer").getString("image");

			// DisplayImageOptions defaultOptions = new
			// DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.infox_person_no_icon)
			// .showImageOnFail(R.drawable.infox_person_no_icon).showImageOnLoading(R.drawable.infox_person_no_icon)
			// .cacheInMemory(true).cacheOnDisc(true).build();
			// ImageLoader.getInstance().displayImage(imageurl,
			// holder.itemimage, defaultOptions);

			AppKit.SetUserImg(imageurl, holder.itemimage);
			// if (array.getJSONObject(position).getInt("type") == 0) {
			// BadgeView badge = new BadgeView(activity, holder.linlayout);
			// badge.setText("new");
			// badge.setTextSize(11);
			// badge.setGravity(Gravity.CENTER);
			// badge.setBackgroundResource(R.drawable.infox_orange_roungbg);
			// badge.setTextColor(Color.parseColor("#fa9600"));
			// badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
			// badge.show(true);
			// }
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// holder.itemMassage.setTextSize(SharedPreferencesKit.getFloat(activity,
		// Const.SP_TEXTSIZE, Const.noramlSize));
		return convertView;
	}

	static class ViewHolder {

		ImageView itemimage;
		TextView itemMassagetime;
		TextView itemMassageFrom;
		TextView itemMassageData;
		RelativeLayout linlayout;

	}

	/**
	 * 替换当前数据源数据
	 * 
	 * @param moreArry
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:23
	 */
	public void setData(JSONArray jsonArray) {
		array = jsonArray;
		notifyDataSetChanged();
	}

	/**
	 * 向已有数据源中追加数据
	 * 
	 * @param jsonArray
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:00
	 */
	public void appendData(JSONArray jsonArray) {
		try {
			array = AppKit.joinJSONArray(array, jsonArray);
		} catch (JSONException e) {
			// Log.e(TAG, e.getMessage(), e);
		}
		notifyDataSetChanged();
	}
}
